/**
 * Created by 枝枝_zhizhi on 2017/7/20.
 */
var app = getApp();
var utils = require('../../utils/utils');
var Zan = require('../../components/zanui/index');
Page(Object.assign({}, Zan.TopTips, {
    data: {
        userid: '',
        userdetails: '',
        nickname: '',
        mobilePhone: '',
        address: '',
        amend: false,  //判断用户是否做出过修改
        wxName:""
    },

    onLoad: function () {
        this.getUserdetails();
        this.setData({
            userid: app.globalData.userid
        })
    },
   onShow:function(){
       this.setData({
           wxName: app.globalData.userInfo.nickName
       })
   },

    /**
     * 获取会员个人信息
     */
    getUserdetails: function () {
        var that = this;
        var httpObj = {
            url: 'userother/userdetails',
            data: {
                who: app.globalData.who,
                userid: app.globalData.userid
            },
            success: function (res) {
                var data=res.data;
                if(data.userdetails.nickname==null){
                    console.log('会员昵称为null');
                    console.log(data.userdetails.nickname);
                    data.userdetails.nickname=app.globalData.userInfo.nickName;
                }
                that.setData({
                    userdetails:data.userdetails
                })
                console.log('会员信息');

                console.log(res.data);
            }

        }
        utils.http(httpObj);
    },


    /**
     * 获取修改的名字
     * @param e
     */
    bindName: function (e) {
        this.setData({
            nickname: e.detail.value,
            amend: true
        })
    },

    /**
     * 获取修改的电话
     * @param e
     */
    bindTel: function (e) {
        this.setData({
            mobilePhone: e.detail.value,
            amend: true
        })
    },

    /**
     * 获取修改的地址
     * @param e
     */
    bindAddress: function (e) {
        this.setData({
            address: e.detail.value,
            amend: true
        })
    },



    /**
     * 保存会员个人信息
     */
    save: function () {
        var that = this;
        var data = {
            who: app.globalData.who,
            userid: that.data.userid,
            nickname: that.data.nickname || that.data.userdetails.nickname,
            mobilePhone: that.data.mobilePhone || that.data.userdetails.mobile,
            address: that.data.address || that.data.userdetails.address
        }
        if ((data.nickname == this.data.userdetails.nickname && data.mobilePhone == this.data.userdetails.mobile && data.address == this.data.userdetails.address) || !this.data.amend) {
            this.showZanTopTips('没有修改任何信息');
            return;
        }
        var httpObj = {
            url: 'userother/userdetailssave',
            method: 'POST',
            data: data,
            success: function (res) {
                wx.showToast({
                    title: '修改成功',
                    icon: 'success',
                    success: function () {
                        that.setData({
                            nickname: '',
                            mobilePhone: '',
                            address: '',
                            amend: false,
                        })
                        that.getUserdetails();
                    }
                })
            },
            fail: function () {
                wx.showToast({
                    title: '修改失败',
                    icon: 'loading',
                })
            }
        };
        console.log("请求参数");
        console.log(httpObj);
        utils.http(httpObj);
    },
    //监听下拉
    onPullDownRefresh:function(){
        console.log("下拉刷新");
        wx.stopPullDownRefresh();
    }
}))